package com.lj.demo.statisticsService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.demo.servicebase.exception.AException;
import com.lj.demo.statisticsService.client.UcenterClient;
import com.lj.demo.statisticsService.entity.StatisticsDaily;
import com.lj.demo.statisticsService.mapper.StatisticsDailyMapper;
import com.lj.demo.statisticsService.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2021-01-17
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    //统计传入日期的注册人数加入数据库
    @Override
    public void createStatisticByDate(String day) {
        if (("null").equals(day) || StringUtils.isEmpty(day)){
            throw new AException(20001,"请输入正确的日期");
        }
        //删除已经存在的统计对象
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper();
        queryWrapper.eq("date_calculated",day);
        baseMapper.delete(queryWrapper);

        //统计对象
        Integer countNum = (Integer) ucenterClient.countRegister(day).getData().get("count");
        Integer loginNum = RandomUtils.nextInt(100, 200); //todo 登录人数
        Integer videoViewNum = RandomUtils.nextInt(100, 200); //todo
        Integer courseNum = RandomUtils.nextInt(100, 200);  //todo

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        baseMapper.insert(daily);
    }

    /**
     * 获取区间统计的信息
     * @param begin 开始时间
     * @param end 结束时间
     * @param type 类型
     * @return
     */
    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        if (StringUtils.isEmpty(begin) || StringUtils.isEmpty(end) || StringUtils.isEmpty(type)){
            throw new AException(20001,"请输入正确的日期");
        }
        //查询数据
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(type,"date_calculated");
        queryWrapper.between("date_calculated",begin,end);
        List<StatisticsDaily> dayLists = baseMapper.selectList(queryWrapper);
        
        Map<String,Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();

        for (StatisticsDaily dailyList :dayLists){
            dateList.add(dailyList.getDateCalculated());
            switch (type){
                case "register_num":
                    dataList.add(dailyList.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(dailyList.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(dailyList.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(dailyList.getCourseNum());
                    break;
                default:
                    break;
            }

        }
        map.put("dataList",dataList);
        map.put("dateList",dateList);
        return map;
    }
}
